package com.zdxlz.fcmp.common.security.service;

import com.zdxlz.fcmp.common.core.constant.RedisKeyConstants;
import com.zdxlz.fcmp.common.core.exception.BizException;
import com.zdxlz.fcmp.common.core.util.RedisUtil;
import com.zdxlz.fcmp.common.security.config.JwtConfig;
import com.zdxlz.fcmp.common.security.util.JwtTokenUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @className RedisAuthServiceImpl
 * @author: aman
 * @date 2025/2/5 17:49
 * @description 
 */

@Component
@AllArgsConstructor
@Slf4j
public class RedisAuthServiceImpl {
    private final RedisUtil redisUtil;
    private final JwtConfig jwtConfig;

    // refreshToken 存储到 redis 并设置过期时间
    public void cacheRefreshToken(String refreshToken, Long version) {
        String refreshKey = String.format(RedisKeyConstants.AUTH_REFRESH_TOKEN, JwtTokenUtil.getSignature(refreshToken));
        redisUtil.set(refreshKey, version, jwtConfig.getRefreshTokenExpiration(), TimeUnit.SECONDS);

    }


    // 删除refreshToken
    public void clearRefreshToken(String refreshToken) {
        String refreshKey = String.format(RedisKeyConstants.AUTH_REFRESH_TOKEN, JwtTokenUtil.getSignature(refreshToken));
        redisUtil.delete(refreshKey);
    }

    // accessToken 存储到 redis 并设置过期时间
    public void cacheAccessToken(String mobile, String accessToken, Long version) {
        String accessKey = String.format(RedisKeyConstants.AUTH_ACCESS_TOKEN, mobile, JwtTokenUtil.getSignature(accessToken));
        redisUtil.set(accessKey, version, jwtConfig.getAccessTokenExpiration(), TimeUnit.SECONDS);
    }


    // 删除accessToken
    public void clearAccessToken(String mobile, String accessToken) {
        String accessKey = String.format(RedisKeyConstants.AUTH_ACCESS_TOKEN, mobile, JwtTokenUtil.getSignature(accessToken));
        redisUtil.delete(accessKey);
    }

    // 根据 Refresh Token 获取用户信息
    public Long getVersionRefreshToken(String refreshToken) {
        String refreshKey = String.format(RedisKeyConstants.AUTH_REFRESH_TOKEN, JwtTokenUtil.getSignature(refreshToken));


        Object object = redisUtil.get(refreshKey);
        if (object == null) {
            log.error("Redis中查询RefreshToken不存在！");

            throw new BizException("Redis中不存在RefreshToken！");
        }
        return Long.valueOf(object.toString());

    }

    // 根据 Refresh Token 获取用户信息
    public Long getVersionAccessToken(String mobile, String accessToken) {
        String accessKey = String.format(RedisKeyConstants.AUTH_ACCESS_TOKEN, mobile, JwtTokenUtil.getSignature(accessToken));


        Object object = redisUtil.get(accessKey);
        if (object == null) {
            log.error("Redis中查询【{}】accessToken不存在！", mobile);
            throw new BizException("Redis中不存在AccessToken！");
        }
        return Long.valueOf(object.toString());

    }

    // 检查 Token 是否有效
    public boolean isExistRefreshToken(String token) {
        String key = String.format(RedisKeyConstants.AUTH_REFRESH_TOKEN, JwtTokenUtil.getSignature(token));
        return redisUtil.hasKey(key);
    }

    public boolean isExistAccessToken(String mobile, String token) {
        String accessKey = String.format(RedisKeyConstants.AUTH_ACCESS_TOKEN, mobile, JwtTokenUtil.getSignature(token));
        return redisUtil.hasKey(accessKey);
    }

    // version 自增并返回
    public Long incrementVersion(String mobile) {
        String key = String.format(RedisKeyConstants.USER_AUTH_VERSION, mobile);
        return redisUtil.increment(key, 1L);
    }

    // version 自增并返回
    public Long getUserVersion(String mobile) {
        String key = String.format(RedisKeyConstants.USER_AUTH_VERSION, mobile);
        Object object = redisUtil.get(key);
        if (object == null) {
            log.info("Redis中不存在该用户【{}】的版本信息！", mobile);
            return 1L;
        }
        return Long.valueOf(object.toString());
    }


}
